package cn.ekuma.epos.datalogic.define.dao;

import java.util.List;

import com.openbravo.bean.Reservation;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.TableDBSentenceBuilder;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import e.odbo.data.dao.ModifiedLogDAO;

public class ReservationDAO  extends ModifiedLogDAO<Reservation>{

	public ReservationDAO(I_Session s,CustomerInfoDAO customerInfoDAO) {
		super(s);
		this.customerInfoDAO=customerInfoDAO;
	}

	public ReservationDAO(I_Session s) {
		super(s);
	}

	CustomerInfoDAO customerInfoDAO;

	@Override
	public TableDefinition getTable() {
		
		return new TableDefinition(
                "RESERVATIONS",
                new Field[]{
                          new Field("ID",Datas.STRING,Formats.STRING),
                          new Field("CREATED",Datas.TIMESTAMP,Formats.TIMESTAMP),
                          new Field("DATENEW",Datas.TIMESTAMP,Formats.TIMESTAMP),
                          new Field("TITLE",Datas.STRING,Formats.STRING),
                          new Field("CHAIRS",Datas.INT,Formats.INT),
                          new Field("ISDONE",Datas.BOOLEAN, Formats.BOOLEAN),
                          new Field("DESCRIPTION",Datas.STRING,Formats.STRING),
                          new Field("CUSTOMER",Datas.STRING,Formats.STRING),
                          new Field("LASTMODIFIED",Datas.TIMESTAMP, Formats.TIMESTAMP)
                },
                new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, Reservation obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setTimestamp(2, obj.getCreatedate());
		dp.setTimestamp(3,obj.getDateNew());
		dp.setString(4,obj.getTitle());
		dp.setInt(5,obj.getChair());
		dp.setBoolean(6,obj.isDone());
		dp.setString(7,obj.getDescription());
		dp.setString(8,obj.getCustomerInfo().getId());
		dp.setTimestamp(9,obj.getLastModified());
		
	}

	@Override
	public Reservation readValues(DataRead dr,Reservation obj) throws BasicException {
		if(obj==null)
			obj=new Reservation();
		obj.setId(dr.getString(1));
		obj.setCreatedate(dr.getTimestamp(2));
		obj.setDateNew(dr.getTimestamp(3));
		obj.setTitle(dr.getString(4));
		obj.setChair(dr.getInt(5));
		obj.setDone(dr.getBoolean(6));
		obj.setDescription(dr.getString(7));
		if(customerInfoDAO!=null)
		  obj.setCustomerInfo(customerInfoDAO.findCustomerInfoByID(dr.getString(8)));
		obj.setLastModified(dr.getTimestamp(9));
		return null;
	}
	
	@Override
	public Class getSuportClass() {
		return Reservation.class;
	}
	

	public List<Reservation> list(Object filter) throws BasicException {
		return new PreparedSentence(s
	            ,((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE DATENEW >= ? AND DATENEW < ?"
	            , new SerializerWriteBasic(new Datas[] {Datas.TIMESTAMP, Datas.TIMESTAMP})
	            , this).list(filter);
	}
}
